<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>项目8：字符游戏-贪吃蛇 - Adbean&#39;s Blog</title><meta name="Description" content="Adbean&#39;s Blog"><meta property="og:title" content="项目8：字符游戏-贪吃蛇" />
<meta property="og:description" content="实验目的 了解字符游戏的表示 体验自顶向下的设计方法实现问题求解 使用伪代码表示算法 使用函数抽象过程 游戏要求与表示 玩法 贪吃蛇游戏是一款经典的益智游" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://adbean.gitee.io/posts/my-eighth-project/" />
<meta property="og:image" content="https://adbean.gitee.io/logo.png"/>
<meta property="article:published_time" content="2020-12-20T15:51:17+08:00" />
<meta property="article:modified_time" content="2020-12-20T15:51:17+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://adbean.gitee.io/logo.png"/>

<meta name="twitter:title" content="项目8：字符游戏-贪吃蛇"/>
<meta name="twitter:description" content="实验目的 了解字符游戏的表示 体验自顶向下的设计方法实现问题求解 使用伪代码表示算法 使用函数抽象过程 游戏要求与表示 玩法 贪吃蛇游戏是一款经典的益智游"/>
<meta name="application-name" content="Adbean&#39;s Blog">
<meta name="apple-mobile-web-app-title" content="Adbean&#39;s Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="icon" href="../../Owl.ico"><link rel="apple-touch-icon" sizes="180x180" href="../../apple-touch-icon.png"><link rel="mask-icon" href="../../safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="../../site.webmanifest"><link rel="canonical" href="https://adbean.gitee.io/posts/my-eighth-project/" /><link rel="prev" href="https://adbean.gitee.io/posts/my-seventh-project/" /><link rel="next" href="https://adbean.gitee.io/posts/my-ninth-project/" /><link rel="stylesheet" href="../../lib/normalize/normalize.min.css"><link rel="stylesheet" href="../../css/style.min.css"><link rel="stylesheet" href="../../lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="../../lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "项目8：字符游戏-贪吃蛇",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/adbean.gitee.io\/posts\/my-eighth-project\/"
        },"genre": "posts","keywords": "项目, C语言, 游戏, 贪吃蛇","wordcount":  1110 ,
        "url": "https:\/\/adbean.gitee.io\/posts\/my-eighth-project\/","datePublished": "2020-12-20T15:51:17+08:00","dateModified": "2020-12-20T15:51:17+08:00","publisher": {
            "@type": "Organization",
            "name": "Adbean"},"author": {
                "@type": "Person",
                "name": "Adbean"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('dark' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'dark' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="../../" title="Adbean&#39;s Blog"><img
        class="lazyload logo"
        src="../../svg/loading.min.svg"
        data-src="../../Owl.ico"
        data-srcset="../../Owl.ico, ../../Owl.ico 1.5x, ../../Owl.ico 2x"
        data-sizes="auto"
        alt="/Owl.ico"
        title="/Owl.ico" />Adbean&#39;s Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="../../"> 主页 </a><a class="menu-item" href="../../posts/"> 文章 </a><a class="menu-item" href="../../posts/my-index/"> 索引 </a><a class="menu-item" href="../../tags/"> 标签 </a><a class="menu-item" href="../../categories/"> 分类 </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="../../" title="Adbean&#39;s Blog"><img
        class="lazyload logo"
        src="../../svg/loading.min.svg"
        data-src="../../Owl.ico"
        data-srcset="../../Owl.ico, ../../Owl.ico 1.5x, ../../Owl.ico 2x"
        data-sizes="auto"
        alt="/Owl.ico"
        title="/Owl.ico" />Adbean&#39;s Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><a class="menu-item" href="../../" title="">主页</a><a class="menu-item" href="../../posts/" title="">文章</a><a class="menu-item" href="../../posts/my-index/" title="">索引</a><a class="menu-item" href="../../tags/" title="">标签</a><a class="menu-item" href="../../categories/" title="">分类</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">项目8：字符游戏-贪吃蛇</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="https://adbean.gitee.io/" title="Author" target="_blank" rel="noopener noreffer author" class="author"><i class="fas fa-user-circle fa-fw"></i>Adbean</a></span>&nbsp;<span class="post-category">收录于 <a href="../../categories/%E9%A1%B9%E7%9B%AE/"><i class="far fa-folder fa-fw"></i>项目</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-12-20">2020-12-20</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1110 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 3 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#实验目的">实验目的</a></li>
    <li><a href="#游戏要求与表示">游戏要求与表示</a>
      <ul>
        <li><a href="#玩法">玩法</a></li>
        <li><a href="#游戏表示">游戏表示</a></li>
      </ul>
    </li>
    <li><a href="#编程要求">编程要求</a>
      <ul>
        <li><a href="#任务-1会动的蛇">任务 1：会动的蛇</a></li>
        <li><a href="#任务-2会吃的蛇">任务 2：会吃的蛇</a></li>
      </ul>
    </li>
    <li><a href="#完整代码">完整代码</a>
      <ul>
        <li><a href="#编译环境">编译环境</a></li>
        <li><a href="#运行效果">运行效果</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="实验目的">实验目的</h2>
<ol>
<li>了解<strong>字符游戏</strong>的表示</li>
<li>体验<strong>自顶向下</strong>的设计方法实现问题求解</li>
<li>使用<strong>伪代码</strong>表示算法</li>
<li>使用<strong>函数</strong>抽象过程</li>
</ol>
<h2 id="游戏要求与表示">游戏要求与表示</h2>
<h3 id="玩法">玩法</h3>
<p><a href="https://baike.baidu.com/item/%E8%B4%AA%E5%90%83%E8%9B%87/9510203?fr=aladdin" target="_blank" rel="noopener noreffer"><strong>贪吃蛇</strong></a>游戏是一款经典的益智游戏，有 PC 和手机等多平台版本。既简单又耐玩。该游戏通过控制蛇头方向吃蛋，从而使得蛇变得越来越长。</p>
<h3 id="游戏表示">游戏表示</h3>
<p>给定一个 $10\times 10$ 的<strong>字符矩阵</strong>表示蛇的生存空间,其中有一条长度 5 的蛇(HXXXX), &ldquo;H&quot;表示蛇头, &ldquo;X&quot;表示蛇身体。</p>
<p>空间中可能有食物（&quot;$&quot;表示）和障碍物（&quot;*&ldquo;表示）</p>
<p>你可以使用 &ldquo;<strong>WASD</strong>&rdquo; 按键分别控制蛇的前进方向&rdquo;<strong>上左下右</strong>&rdquo;, 当蛇头<strong>碰到自己的身体</strong>或<strong>走出边界</strong>, 游戏结束, 否则蛇按你指定方向<strong>前进一步</strong>。</p>
<h2 id="编程要求">编程要求</h2>
<h3 id="任务-1会动的蛇">任务 1：会动的蛇</h3>
<h4 id="程序头部要求">程序头部要求</h4>
<p>定义良好的头部，将使得程序更加<strong>易于阅读</strong>，<strong>易于维护</strong>。</p>
<p>包括：<strong>常数定义</strong>、<strong>函数定义</strong>等。</p>
<p>以下是我的贪吃蛇程序的<strong>头部</strong>：</p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../Project8/program.png"
        data-srcset="../../Project8/program.png, ../../Project8/program.png 1.5x, ../../Project8/program.png 2x"
        data-sizes="auto"
        alt="/Project8/program.png"
        title="program header" /></p>
<h5 id="代码规范">代码规范</h5>
<ul>
<li>使用 ANSI C 的 <strong>C99</strong> 标准，无任何 warning</li>
<li>符合 Google 代码风格</li>
</ul>
<h5 id="程序结构">程序结构</h5>
<p>程序总体结构符合<strong>伪代码</strong>框架</p>
<div class="highlight"><pre class="chroma"><code class="language-c" data-lang="c"><span class="err">输出字符矩阵</span>
<span class="n">WHILE</span> <span class="n">not</span> <span class="err">游戏结束</span> <span class="n">DO</span>
    <span class="n">ch</span><span class="err">＝等待输入</span>
    <span class="n">CASE</span> <span class="n">ch</span> <span class="n">DO</span>
    <span class="err">‘</span><span class="n">A</span><span class="err">’</span><span class="o">:</span><span class="err">左前进一步，</span><span class="k">break</span>
    <span class="err">‘</span><span class="n">D</span><span class="err">’</span><span class="o">:</span><span class="err">右前进一步，</span><span class="k">break</span>
    <span class="err">‘</span><span class="n">W</span><span class="err">’</span><span class="o">:</span><span class="err">上前进一步，</span><span class="k">break</span>
    <span class="err">‘</span><span class="n">S</span><span class="err">’</span><span class="o">:</span><span class="err">下前进一步，</span><span class="k">break</span>
    <span class="n">END</span> <span class="n">CASE</span>
    <span class="err">输出字符矩阵</span>
<span class="n">END</span> <span class="n">WHILE</span>
<span class="err">输出</span> <span class="n">Game</span> <span class="n">Over</span><span class="o">!!!</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../Project8/main.png"
        data-srcset="../../Project8/main.png, ../../Project8/main.png 1.5x, ../../Project8/main.png 2x"
        data-sizes="auto"
        alt="/Project8/main.png"
        title="program main" /></p>
<h3 id="任务-2会吃的蛇">任务 2：会吃的蛇</h3>
<h4 id="功能需求">功能需求</h4>
<ul>
<li>snake 头撞到身体、障碍（边界或你在地图中定义）游戏结束</li>
<li>snake 头吃到食物，snake 就长一节</li>
</ul>
<div class="highlight"><pre class="chroma"><code class="language-c" data-lang="c"><span class="c1">// make the snake move
</span><span class="c1"></span><span class="kt">void</span> <span class="nf">SnakeMove</span><span class="p">(</span><span class="kt">int</span> <span class="n">dx</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dy</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">newTailX</span> <span class="o">=</span> <span class="n">snakeCol</span><span class="p">[</span><span class="n">TAIL</span><span class="p">];</span>
    <span class="kt">int</span> <span class="n">newTailY</span> <span class="o">=</span> <span class="n">snakeRow</span><span class="p">[</span><span class="n">TAIL</span><span class="p">];</span>

    <span class="c1">// row: snakeRowY, column: snakeCol
</span><span class="c1"></span>    <span class="n">map</span><span class="p">[</span><span class="n">snakeRow</span><span class="p">[</span><span class="n">TAIL</span><span class="p">]][</span><span class="n">snakeCol</span><span class="p">[</span><span class="n">TAIL</span><span class="p">]]</span> <span class="o">=</span> <span class="n">BLANK_CELL</span><span class="p">;</span>
    <span class="n">map</span><span class="p">[</span><span class="n">snakeRow</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]][</span><span class="n">snakeCol</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]]</span> <span class="o">=</span> <span class="n">SNAKE_BODY</span><span class="p">;</span>

    <span class="c1">// move forward the body
</span><span class="c1"></span>    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">body</span> <span class="o">=</span> <span class="n">TAIL</span><span class="p">;</span> <span class="n">body</span> <span class="o">&lt;</span> <span class="n">HEAD</span><span class="p">;</span> <span class="n">body</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">snakeCol</span><span class="p">[</span><span class="n">body</span><span class="p">]</span> <span class="o">=</span> <span class="n">snakeCol</span><span class="p">[</span><span class="n">body</span> <span class="o">+</span> <span class="mi">1</span><span class="p">];</span>
        <span class="n">snakeRow</span><span class="p">[</span><span class="n">body</span><span class="p">]</span> <span class="o">=</span> <span class="n">snakeRow</span><span class="p">[</span><span class="n">body</span> <span class="o">+</span> <span class="mi">1</span><span class="p">];</span>
    <span class="p">}</span>

    <span class="n">snakeCol</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dx</span><span class="p">;</span>
    <span class="n">snakeRow</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]</span> <span class="o">+=</span> <span class="n">dy</span><span class="p">;</span>
    <span class="n">map</span><span class="p">[</span><span class="n">snakeRow</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]][</span><span class="n">snakeCol</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]]</span> <span class="o">=</span> <span class="n">SNAKE_HEAD</span><span class="p">;</span>

    <span class="c1">// check whether the snake eats the food
</span><span class="c1"></span>    <span class="k">if</span> <span class="p">(</span><span class="n">isEaten</span><span class="p">())</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">body</span> <span class="o">=</span> <span class="n">HEAD</span><span class="p">;</span> <span class="n">body</span> <span class="o">&gt;=</span> <span class="n">TAIL</span><span class="p">;</span> <span class="n">body</span><span class="o">--</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">snakeCol</span><span class="p">[</span><span class="n">body</span><span class="p">]</span> <span class="o">=</span> <span class="n">snakeCol</span><span class="p">[</span><span class="n">body</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span>
            <span class="n">snakeRow</span><span class="p">[</span><span class="n">body</span><span class="p">]</span> <span class="o">=</span> <span class="n">snakeRow</span><span class="p">[</span><span class="n">body</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span>
        <span class="p">}</span>
        <span class="n">snakeCol</span><span class="p">[</span><span class="n">TAIL</span><span class="p">]</span> <span class="o">=</span> <span class="n">newTailX</span><span class="p">;</span>
        <span class="n">snakeRow</span><span class="p">[</span><span class="n">TAIL</span><span class="p">]</span> <span class="o">=</span> <span class="n">newTailY</span><span class="p">;</span>
        <span class="n">map</span><span class="p">[</span><span class="n">newTailY</span><span class="p">][</span><span class="n">newTailX</span><span class="p">]</span> <span class="o">=</span> <span class="n">SNAKE_BODY</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">clearTerminal</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div><h4 id="细化并完善随机放置食物的伪代码">细化并完善随机放置食物的伪代码</h4>
<ul>
<li>找一个空白位置</li>
<li>在该位置放置食物</li>
</ul>
<div class="highlight"><pre class="chroma"><code class="language-c" data-lang="c"><span class="c1">// create the food on a random blank cell
</span><span class="c1"></span><span class="kt">void</span> <span class="nf">CreateFood</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">do</span>
    <span class="p">{</span>
        <span class="n">foodRow</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">rand</span><span class="p">()</span> <span class="o">%</span> <span class="mi">10</span><span class="p">;</span>          <span class="c1">// random number from [1, 10]
</span><span class="c1"></span>        <span class="n">foodCol</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">rand</span><span class="p">()</span> <span class="o">%</span> <span class="mi">10</span><span class="p">;</span>          <span class="c1">// random number from [1, 10]
</span><span class="c1"></span>    <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="n">foodRow</span><span class="p">][</span><span class="n">foodCol</span><span class="p">]</span> <span class="o">!=</span> <span class="sc">&#39; &#39;</span><span class="p">);</span> <span class="c1">// make sure that the food is created on a blank cell
</span><span class="c1"></span>
    <span class="n">map</span><span class="p">[</span><span class="n">foodRow</span><span class="p">][</span><span class="n">foodCol</span><span class="p">]</span> <span class="o">=</span> <span class="n">FOOD</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h4 id="需要进一步细化的代码">需要进一步细化的代码</h4>
<ul>
<li>蛇头撞到身体、障碍物 … …</li>
<li>蛇头撞到食物 … …</li>
<li>蛇头进入一个空位置 … …</li>
</ul>
<div class="highlight"><pre class="chroma"><code class="language-c" data-lang="c"><span class="c1">// check whether the snake eats the food:
</span><span class="c1"></span><span class="kt">bool</span> <span class="nf">isEaten</span><span class="p">()</span>
<span class="p">{</span>
    <span class="kt">bool</span> <span class="n">isEaten</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
    <span class="c1">// if the snake has eaten the food, then its strength is increased
</span><span class="c1"></span>    <span class="k">if</span> <span class="p">((</span><span class="n">snakeCol</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]</span> <span class="o">==</span> <span class="n">foodCol</span> <span class="o">&amp;&amp;</span> <span class="n">snakeRow</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]</span> <span class="o">==</span> <span class="n">foodRow</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="n">CreateFood</span><span class="p">();</span>
        <span class="n">isEaten</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
        <span class="n">snakeLength</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">isEaten</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// return true if the snake hit itself of hit the wall, else return false
</span><span class="c1"></span><span class="kt">bool</span> <span class="nf">checkHitItself</span><span class="p">()</span>
<span class="p">{</span>
    <span class="kt">bool</span> <span class="n">hitBody</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
    <span class="c1">// check whether the snake head if touches the body
</span><span class="c1"></span>    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">body</span> <span class="o">=</span> <span class="n">TAIL</span><span class="p">;</span> <span class="n">body</span> <span class="o">&lt;</span> <span class="n">HEAD</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">body</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">snakeRow</span><span class="p">[</span><span class="n">body</span><span class="p">]</span> <span class="o">==</span> <span class="n">snakeRow</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="n">snakeCol</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]</span> <span class="o">==</span> <span class="n">snakeCol</span><span class="p">[</span><span class="n">body</span><span class="p">])</span>
        <span class="p">{</span>
            <span class="n">hitBody</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="c1">// check whether the snake head if touches the wall
</span><span class="c1"></span>    <span class="k">return</span> <span class="n">hitBody</span> <span class="o">||</span> <span class="o">!</span><span class="n">checkNotHitWall</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div><h2 id="完整代码">完整代码</h2>
<p>完整代码见 <a href="https://gitee.com/adbean/my-greedy-snake" target="_blank" rel="noopener noreffer"><strong>gitee 仓库</strong></a>：<a href="https://gitee.com/adbean/my-greedy-snake">https://gitee.com/adbean/my-greedy-snake</a></p>
<h3 id="编译环境">编译环境</h3>
<ul>
<li><strong>Ubuntu</strong> 20.04.1 LTS (Windows Subsystem for Linux (<strong>WSL</strong>) in Windows10)</li>
<li><strong>gcc</strong> version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)</li>
</ul>
<blockquote>
<p>由于调用了 linux 内核中的<strong>清屏</strong>以达到<strong>刷新效果</strong>，若在 windows 系统下运行，请使用 system(&ldquo;cls&rdquo;)</p>
</blockquote>
<h3 id="运行效果">运行效果</h3>
<p>每次使用 <strong>WASD</strong> 进行<strong>一步移动</strong>，每当贪吃蛇吃到食物后，身体<strong>增长</strong></p>
<p>若撞到<strong>自己</strong>或是<strong>墙壁</strong>，则<strong>游戏结束</strong></p>
<p>在游戏<strong>正常运行</strong>的基础上，加入了<strong>暂停</strong>功能</p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../Project8/snake.gif"
        data-srcset="../../Project8/snake.gif, ../../Project8/snake.gif 1.5x, ../../Project8/snake.gif 2x"
        data-sizes="auto"
        alt="/Project8/snake.gif"
        title="program snake" /></p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../Project8/snakePause.gif"
        data-srcset="../../Project8/snakePause.gif, ../../Project8/snakePause.gif 1.5x, ../../Project8/snakePause.gif 2x"
        data-sizes="auto"
        alt="/Project8/snakePause.gif"
        title="program snake pause" /></p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-12-20</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="../../tags/%E9%A1%B9%E7%9B%AE/">项目</a>,&nbsp;<a href="../../tags/c%E8%AF%AD%E8%A8%80/">C语言</a>,&nbsp;<a href="../../tags/%E6%B8%B8%E6%88%8F/">游戏</a>,&nbsp;<a href="../../tags/%E8%B4%AA%E5%90%83%E8%9B%87/">贪吃蛇</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="../../">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="../../posts/my-seventh-project/" class="prev" rel="prev" title="IT 伦理与道德 - 信息爆炸时代，如何保护你的个人信息安全"><i class="fas fa-angle-left fa-fw"></i>IT 伦理与道德 - 信息爆炸时代，如何保护你的个人信息安全</a>
            <a href="../../posts/my-ninth-project/" class="next" rel="next" title="项目9：字符游戏-智能蛇">项目9：字符游戏-智能蛇<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.74.3">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://adbean.gitee.io/" target="_blank">Adbean</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="../../lib/katex/katex.min.css"><link rel="stylesheet" href="../../lib/katex/copy-tex.min.css"><script type="text/javascript" src="../../lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="../../lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="../../lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="../../lib/katex/katex.min.js"></script><script type="text/javascript" src="../../lib/katex/auto-render.min.js"></script><script type="text/javascript" src="../../lib/katex/copy-tex.min.js"></script><script type="text/javascript" src="../../lib/katex/mhchem.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false}};</script><script type="text/javascript" src="../../js/theme.min.js"></script></body>
</html>
